package com.bl.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeUtil {
    public static String HandlerTime(Date time) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date now;
        try {
            now = new Date();
//            now = df.parse(new Date().toString());
//            java.util.Date date = df.parse("2004-01-02 11:30:24");
            long l = now.getTime() - time.getTime();
            long day = l / (24 * 60 * 60 * 1000);
            long hour = (l / (60 * 60 * 1000) - day * 24);
            long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
            long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);

            StringBuffer sb = new StringBuffer();

            if (day > 0) {
                if (day>365){
                    long year = day / 365;
                    sb.append(year + "年前");
                    return sb.toString();
                }else if(day>30){
                    long month = day / 30;
                    sb.append(month + "月前");
                    return sb.toString();
                }


                sb.append(day + "天前");
                return sb.toString();
            }
            if (hour > 0) {
                sb.append(hour + "小时前");
                return sb.toString();
            }
            if (min > 0) {
                sb.append(min + "分前");
                return sb.toString();
            }
            sb.append(s + "秒前");
            System.out.println(sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
